{% set pypi_name = 'masakari' %}
{% set source = fetch_source('https://tarballs.openstack.org/masakari/masakari-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}

Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Summary:        Virtual Machine High Availability (VMHA) service for OpenStack
License:        {{ license('Apache-2.0') }}
Group:          Development/Languages/Python
URL:            https://docs.openstack.org/masakari
Source0:        {{ source|basename }}
Source1:        openstack-masakari-api.service
Source2:        openstack-masakari-engine.service
Source3:        openstack-masakari.tmpfiles
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('ddt') }}
BuildRequires:  {{ py3('eventlet') }}
BuildRequires:  {{ py3('keystoneauth1') }}
BuildRequires:  {{ py3('keystonemiddleware') }}
BuildRequires:  {{ py3('microversion_parse') }}
BuildRequires:  {{ py3('python-novaclient') }}
BuildRequires:  {{ py3('oslo.db') }}
BuildRequires:  {{ py3('oslo.messaging') }}
BuildRequires:  {{ py3('oslo.middleware') }}
BuildRequires:  {{ py3('oslo.policy') }}
BuildRequires:  {{ py3('oslo.service') }}
BuildRequires:  {{ py3('oslo.upgradecheck') }}
BuildRequires:  {{ py3('oslo.versionedobjects') }}
BuildRequires:  {{ py3('oslotest') }}
BuildRequires:  {{ py3('python-subunit') }}
BuildRequires:  {{ py3('requests-mock') }}
BuildRequires:  {{ py3('stestr') }}
BuildRequires:  {{ py3('taskflow') }}
Requires:       python3-masakari = %{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  systemd-rpm-macros
Requires(pre):  pwdutils
%else
BuildRequires:  systemd
Requires(post): systemd
Requires(postun): systemd
Requires(pre):    shadow-utils
Requires(preun):  systemd
%endif

%description
Masakari is a service providing high availability for VMs in OpenStack.

%if 0%{?with_doc}
%package        doc
Summary:        Documentation for OpenStack Masakari Service
BuildRequires:  {{ py3('Sphinx') }}
BuildRequires:  {{ py3('oslosphinx') }}

%description    doc
Masakari is a service providing high availability for VMs in OpenStack.

This package contains the documentation.
%endif

%package -n     python3-{{ pypi_name }}
Summary:        Masakari Python libraries
Requires:       {{ py3('Babel') }}
Requires:       {{ py3('iso8601') }}
Requires:       {{ py3('jsonschema') }}
Requires:       {{ py3('keystoneauth1') }}
Requires:       {{ py3('keystonemiddleware') }}
Requires:       {{ py3('microversion_parse') }}
Requires:       {{ py3('python-novaclient') }}
Requires:       {{ py3('oslo.config') }}
Requires:       {{ py3('oslo.db') }}
Requires:       {{ py3('oslo.i18n') }}
Requires:       {{ py3('oslo.log') }}
Requires:       {{ py3('oslo.messaging') }}
Requires:       {{ py3('oslo.middleware') }}
Requires:       {{ py3('oslo.policy') }}
Requires:       {{ py3('oslo.service') }}
Requires:       {{ py3('oslo.upgradecheck') }}
Requires:       {{ py3('oslo.utils') }}
Requires:       {{ py3('oslo.versionedobjects') }}
Requires:       {{ py3('pbr') }}
Requires:       {{ py3('six') }}
Requires:       {{ py3('stevedore') }}
Requires:       {{ py3('taskflow') }}

%description -n python3-{{ pypi_name }}
Masakari is a service providing high availability for VMs in OpenStack.

This package contains the Python libraries.

%package        api
Summary:        OpenStack Masakari API service
Requires:       %{name} = %{version}-%{release}

%description    api
Masakari is a service providing high availability for VMs in OpenStack.

This package contains the REST API.

%package        engine
Summary:        OpenStack Masakari Engine service
Requires:       %{name} = %{version}-%{release}

%description    engine
Masakari is a service providing high availability for VMs in OpenStack.

This package contains the masakari engine, which is one of core services of masakari.

%prep
%autosetup -p1 -n {{ pypi_name }}-{{upstream_version}}
%py_req_cleanup

%build
%{py3_build}
export PYTHONPATH="."
# from tox.ini's testenv:genconfig
oslo-config-generator --config-file etc/masakari/masakari-config-generator.conf
oslopolicy-sample-generator --config-file etc/masakari/masakari-policy-generator.conf

%if 0%{?with_doc}
PBR_VERSION={{ upstream_version }} %sphinx_build -b html doc/source doc/build/html
# remove the sphinx-build leftovers
rm -rf doc/build/html/.{doctrees,buildinfo}
%endif

%install
%{py3_install}

mkdir -p %{buildroot}%{_sysconfdir}/masakari/
mkdir -p %{buildroot}%{_localstatedir}/log/masakari
#systemd unitfiles
install -p -D -m 644 %SOURCE1 %{buildroot}%{_unitdir}/openstack-masakari-api.service
install -p -D -m 644 %SOURCE2 %{buildroot}%{_unitdir}/openstack-masakari-engine.service
install -p -D -m 644 %SOURCE3 %{buildroot}/%{_tmpfilesdir}/%{name}.conf

install -p -D -m 640 etc/masakari/masakari.conf.sample %{buildroot}%{_sysconfdir}/masakari/masakari.conf
install -p -D -m 640 etc/masakari/api-paste.ini %{buildroot}%{_sysconfdir}/masakari/api-paste.ini
install -p -D -m 640 etc/masakari/policy.yaml.sample %{buildroot}%{_sysconfdir}/masakari/policy.yaml
chmod +x %{buildroot}%{_bindir}/masakari*

# remove usr/etc
rm -rf %{buildroot}%{_prefix}%{_sysconfdir}/

%pre
%openstack_pre_user_group_create masakari masakari

%post
%tmpfiles_create %{_tmpfilesdir}/%{name}.conf

%check
# we don't want to check hacking rules
rm masakari/tests/unit/test_hacking.py
%{__python3} -m stestr.cli run

%post api
%systemd_post openstack-masakari-api.service

%preun api
%systemd_preun openstack-masakari-api.service

%postun api
%systemd_postun_with_restart openstack-masakari-api.service

%post engine
%systemd_post openstack-masakari-engine.service

%preun engine
%systemd_preun openstack-masakari-engine.service

%postun engine
%systemd_postun_with_restart openstack-masakari-engine.service

%files
%license LICENSE
%dir %{_sysconfdir}/masakari
%config(noreplace) %attr(0640, root, masakari) %{_sysconfdir}/masakari/*
%dir %attr(750, masakari, masakari) %{_localstatedir}/log/masakari
%_tmpfilesdir/%{name}.conf
%{_bindir}/masakari-manage
%{_bindir}/masakari-status

%files api
%{_bindir}/masakari-api
%{_bindir}/masakari-wsgi
%{_unitdir}/openstack-masakari-api.service

%files engine
%{_bindir}/masakari-engine
%{_unitdir}/openstack-masakari-engine.service

%if 0%{?with_doc}
%files doc
%license LICENSE
%doc doc/build/html
%endif

%files -n python3-{{ pypi_name }}
%{python3_sitelib}/{{ pypi_name }}*

%changelog
